/**
* @Title: orgGrading_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年10月29日 下午4:48:14
* @version V1.0
*/
;
(function(){
	'use strict';
	var app = angular.module('fwa.mainApp');
	app.controller('OrgGradingQueryCtrl', ['$scope','$http','$modal','BaseURL','Modal','InsuranceGradingService','GradingService',
		                                function(scope,$http,$modal,baseUrl,Modal,InsuranceGradingService,GradingService){	
		scope.scoreType = 0;
		scope.id ="";
		scope.name ="";
		InsuranceGradingService.queryInsuranceGradingData(scope.scoreType);
		scope.insuranceGradingService = InsuranceGradingService;
		scope.showDetail = function(id){
			GradingService.insuranceType = 'update';
			GradingService.insuranceGradingData.standardscoreid=id;
			var orgGradingAddModal = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/agreementQuery/grading/orgGrading_add.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller: 'OrgGradingAddOrUpdateQueryCtrl'
		      	});
		};
		//判断选中的值
		scope.checkInsuranceGrading = function(data) {
			angular.forEach(scope.insuranceGradingService.insuranceGradingDatas,function(insuranceGradingData,index){
				if (insuranceGradingData === data){
					GradingService.insuranceGradingData = data;
					insuranceGradingData.checked = true;
				}
				else {
					insuranceGradingData.checked = false;
				}
			});
		}
	}]).controller('OrgGradingAddOrUpdateQueryCtrl', 
			['$scope','$http','BaseURL','$modalInstance','Modal','InsuranceGradingService','GradingService',
	                  function($scope,$http,baseUrl,$modalInstance,Modal,InsuranceGradingService,GradingService){
			$scope.insuranceGradingService = InsuranceGradingService;
			$scope.form = {};
	    	$scope.totalData = [];
	    	var id ="";
	    	if(GradingService.insuranceGradingData){
	    		 id = GradingService.insuranceGradingData.standardscoreid;
	    	}
	    	var name = "";
	    	if (GradingService.insuranceType
	    		&& GradingService.insuranceType === 'update') {
	    		InsuranceGradingService.queryInsuranceGradingInfo(GradingService.insuranceGradingData.standardscoreid).success(function(res) {
	    			$scope.form.insuranceGrading = res.result.scoreName;
	    			name = res.result.scoreName;
	    			$scope.insuranceRows = res.result.dtoList;
	    			$scope.checkedInsuranceRow($scope.insuranceRows[0]);
	    		});
	    	}else{
	    		id ="";
	    	}
	    	//考核内容显示
	    	$scope.insuranceRows = [];
	    	//新增考核内容
	    	$scope.insuranceAdd = function(){
	    		if ($scope.form.insuranceGrading) {
	    			var row = {"scoreName": "","upperValues": "","dtoList":[],"isValid": "0"};
  	    		$scope.insuranceRows.push(row);
	    		}
	    		else {
	    			Modal.alert("请输入评分类别");
	    			return false;
	    		}
	    	}
	    	//选中的考核内容
	    	$scope.insuranceSelectRow = null;
	    	//选中一行考核内容
	    	$scope.checkedInsuranceRow = function(data) {
	    		$scope.insuranceSelectRow = data;
	    		angular.forEach($scope.insuranceRows,function(insuranceData,index){
	    			if (data === insuranceData) {
	    				insuranceData.checked = true;
	    			}
	    			else {
	    				insuranceData.checked = false;
	    			}
	    		});
	    	}
	    	
	    	//删除考核内容
	    	$scope.insuranceDel = function(){
	    		if ($scope.insuranceSelectRow) {
	    			Modal.confirm("确定删除选中的考核内容吗?",function(){
	    				angular.forEach($scope.insuranceRows,function(insuranceData,index){
	    	    			if ($scope.insuranceSelectRow === insuranceData) {
	    	    				insuranceData.isValid = 1;
	    	    				$scope.insuranceSelectRow = null;
	    	    			}
	    	    		});
		    		});
	    		} 
	    		else {
	    			Modal.alert("请选中一项考核内容进行操作!!!");
	    		}
	    		
	    	}
	    	
	    	//新增扣分细则
	    	$scope.markingAdd = function(){
	    		if ($scope.insuranceSelectRow 
	    		 && $scope.insuranceSelectRow.scoreName
	    		 && $scope.insuranceSelectRow.upperValues ) {
	    			var row = {"scoreName": "","lowerValues": "","upperValues": "","fileno": "","isValid": "0"};
  	    		$scope.insuranceSelectRow.dtoList.push(row);
	    		}
	    		else{
	    			Modal.alert("请输入完整的考核内容");
	    			return false;
	    		}
	    	}
	    	
	    	//选中的扣分细则
	    	$scope.markingSelectRow = null;
	    	//选中一行扣分细则
	    	$scope.checkedMarkingRow = function(data) {
	    		$scope.markingSelectRow = data;
	    		angular.forEach($scope.insuranceSelectRow.dtoList,function(markingData,index){
	    			if (data === markingData) {
	    				markingData.checked = true;
	    			}
	    			else {
	    				markingData.checked = false;
	    			}
	    		});
	    	}
	    	//删除扣分细则
	    	$scope.markingDel = function(){
	    		if ($scope.markingSelectRow) {
	    			Modal.confirm("确定删除选中的扣分细则吗?",function(){
	    				angular.forEach($scope.insuranceSelectRow.dtoList,function(markingData,index){
	    	    			if($scope.markingSelectRow === markingData){
	    	    				markingData.isValid = 1;
	    	    				$scope.markingSelectRow = null;
	    	    			}
	    	    		});
		    		});
	    		} 
	    		else {
	    			Modal.alert("请选中一项扣分细则进行操作!!!");
	    		}
	    	}
	    	
	    	//保存
	    	$scope.confirm = function(){
	    		if(!id){
	    			id="0";
	    		}
	    		var firstChild ={};
	    		firstChild ={
						firstNodeId:id,
						secondNodeId:null,
						thirdNodeId:null,
						standardscoreid:id,
						scoreName:$scope.form.insuranceGrading,
						upperValues:null,
						lowerValues:null,
						scoreType:0,
						isValid:0
				}
				$scope.totalData.push(firstChild);
	    		if ($scope.form.insuranceGrading 
	    			&& $scope.insuranceRows
	    			&& $scope.insuranceRows.length > 0) {
	    			var checkContent = false;
	    			var checkDetail = false;
					angular.forEach($scope.insuranceRows, function(insuranceRows, i) {
						var tempRows = {};
						var parentid1 = insuranceRows.parentid;
						var standardscoreid1 = insuranceRows.standardscoreid;
						if(!standardscoreid1){
							standardscoreid1 = i;
						}
						var scoreName1 = insuranceRows.scoreName;
						var lowerValues1 = insuranceRows.lowerValues;
						var upperValues1 = insuranceRows.upperValues;
						var dtoList = insuranceRows.dtoList;
						var isValid = insuranceRows.isValid;
						if(!scoreName1 && isValid==0){
							checkContent=true;
						}
						tempRows ={
								firstNodeId:id,
								secondNodeId:standardscoreid1,
								thirdNodeId:null,
								standardscoreid:standardscoreid1,
								scoreName:scoreName1,
								upperValues:upperValues1,
								lowerValues:lowerValues1,
								scoreType:0,
								isValid:isValid
						}
						if(standardscoreid1 == i && isValid==1){
						}else{
							$scope.totalData.push(tempRows);
						}
						angular.forEach(dtoList, function(dto, i) {
							var tempRows = {};
							var parentid2 = dto.parentid;
							var standardscoreid2 = dto.standardscoreid;
							if(!standardscoreid2){
								standardscoreid2 = i;
							}
							var scoreName2 = dto.scoreName;
							var lowerValues2 = dto.lowerValues;
							var upperValues2 = dto.upperValues;
							var fileno =  dto.fileno;
							var isValid = dto.isValid;
							if(!scoreName2 && isValid==0){
								checkDetail = true;
							}
							tempRows ={
									firstNodeId:id,
									secondNodeId:standardscoreid1,
									thirdNodeId:standardscoreid2,
									standardscoreid:standardscoreid2,
									scoreName:scoreName2,
									upperValues:upperValues2,
									lowerValues:lowerValues2,
									scoreType:0,
									fileno:fileno,
									isValid:isValid
							}
							if(standardscoreid2 == i && isValid==1){
							}else{
								$scope.totalData.push(tempRows);
							}
						});
					});
					if(checkContent){
						$scope.totalData=[];
						Modal.alert("请输入考核内容！");
						return;
					}
					if(checkDetail){
						$scope.totalData=[];
						Modal.alert("请输入扣分细则！");
						return;
					}
					$scope.scoreType = 0;
					InsuranceGradingService.loading = true;
					$http.post(baseUrl + '/hcp/hcp/standardscore/save.do',$scope.totalData).success(function(data) {
						if(data.msg){
							Modal.alert(data.msg);
						}
						InsuranceGradingService.queryInsuranceGradingData($scope.scoreType);
						 GradingService.insuranceGradingData.standardscoreid ="";
						$modalInstance.dismiss();
						InsuranceGradingService.loading = false;
					});
	    		}
	    		else {
	    			Modal.alert("评分信息不能为空");
	    		}
	    	}
	    	//关闭
	    	$scope.onOk = function(){
	    		$modalInstance.dismiss();
	    	}
	      }]);
	

})();